package com.atguigu.gmall.realtime.app.dws;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.app.fun.DimAsyncFunction;
import com.atguigu.gmall.realtime.bean.TradeProvinceOrderWindow;
import com.atguigu.gmall.realtime.utils.MyClickHouseUtil;
import com.atguigu.gmall.realtime.utils.MyKafkaUtil;
import com.atguigu.gmall.realtime.utils.WindowFunctionUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.common.functions.RichFlatMapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.common.time.Time;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.AsyncDataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.async.AsyncFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.math.BigDecimal;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: Dws10_TradeProvinceOrderWindow
 * Package: com.atguigu.gmall.realtime.app.dws
 * Description:       交易域省份粒度下单各窗口汇总表
 *            从 Kafka 读取业务数据，筛选订单表数据，统计各省份各窗口订单数和订单金额，将数据写入 ClickHouse 交易域省份粒度下单各窗口汇总表。
 * @Author ChenJun(有志男青年)
 * @Create 2023/5/12 8:57
 * @Version 1.0
 */
//数据流：web/app -> Mysql -> Maxwell -> Kafka(ODS) -> FlinkApp -> Kafka(DWD) -> FlinkApp -> Clickhouse(DWS)
//程 序：Mock -> Mysql -> Maxwell -> Kafka(ZK) -> Dwd03_TradeOrderDetail(02) -> Kafka(ZK) -> Dws10_TradeProvinceOrderWindow
// -> Clickhouese(ZK)
public class Dws10_TradeProvinceOrderWindow_01 {
    public static void main(String[] args) throws Exception {

        //TODO 1.获取运行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // TODO 配置环境信息生产环境得有

//         env.enableCheckpointing(3000L, CheckpointingMode.EXACTLY_ONCE);
//        env.getCheckpointConfig().setCheckpointTimeout(60 * 1000L);
//        env.getCheckpointConfig().setMinPauseBetweenCheckpoints(3000L);
//        env.getCheckpointConfig().enableExternalizedCheckpoints(
//                CheckpointConfig.ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION
//        );
//        env.setRestartStrategy(RestartStrategies.failureRateRestart(
//                10, Time.of(1L, TimeUnit.DAYS), Time.of(3L, TimeUnit.MINUTES)
//        ));
//        env.setStateBackend(new HashMapStateBackend());
//        env.getCheckpointConfig().setCheckpointStorage("hdfs://hadoop102:8020/gmall/ck");
//        System.setProperty("HADOOP_USER_NAME", "atguigu");

        //TODO 2.从kafka dwd 层读取 dwd_trade_order_detail 主题数据
        DataStreamSource<String> kafkaDS = env.fromSource(MyKafkaUtil.getKafkaSource("dwd_trade_order_detail", "dws10_tradeprovinceorderwindow"), WatermarkStrategy.noWatermarks(), "kafka-source");

        //TODO 3.过滤null数据 转为json格式
        SingleOutputStreamOperator<JSONObject> jsonObjDS = kafkaDS.flatMap(new FlatMapFunction<String, JSONObject>() {
            @Override
            public void flatMap(String value, Collector<JSONObject> out) throws Exception {
                if (value != null) {
                    out.collect(JSONObject.parseObject(value));
                }
            }
        });
        
        //TODO 4.提取时间戳
        SingleOutputStreamOperator<JSONObject> jsonObjWithWMDS = jsonObjDS.assignTimestampsAndWatermarks(WatermarkStrategy.<JSONObject>forBoundedOutOfOrderness(Duration.ofSeconds(2)).withTimestampAssigner(new SerializableTimestampAssigner<JSONObject>() {
            @Override
            public long extractTimestamp(JSONObject element, long recordTimestamp) {
                return element.getLong("create_time");
            }
        }));

        //TODO 5.按照订单明细id分组去重由left join产生的重复数据,并转换为JavaBean对象
        SingleOutputStreamOperator<TradeProvinceOrderWindow> tradeProvinceDS =
                jsonObjWithWMDS.keyBy(json -> json.getString("id"))
                        .flatMap(new RichFlatMapFunction<JSONObject, TradeProvinceOrderWindow>() {
            
            private ValueState<String> valueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                ValueStateDescriptor<String> stateDescriptor = new ValueStateDescriptor<>("map-state", String.class);
                StateTtlConfig ttlConfig = new StateTtlConfig.Builder(Time.seconds(5))
                        .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite)
                        .build();
                stateDescriptor.enableTimeToLive(ttlConfig);
                valueState = getRuntimeContext().getState(stateDescriptor);
            }

            @Override
            public void flatMap(JSONObject value, Collector<TradeProvinceOrderWindow> out) throws Exception {

                String lastValue = valueState.value();

                if (lastValue == null) {
                    valueState.update("1");

                    BigDecimal orderAmount = new BigDecimal(value.getString("split_total_amount"));
                    out.collect(TradeProvinceOrderWindow.builder()
                            .provinceId(value.getString("province_id"))
                            .orderId(value.getString("order_id"))
                            .orderAmount(orderAmount)
                            .build());
                }
            }
        });

        //TODO 6.按照order_id分组 并统计订单数
        SingleOutputStreamOperator<TradeProvinceOrderWindow> tradeProvinceWithOrderCtDS = tradeProvinceDS.keyBy(TradeProvinceOrderWindow::getOrderId).map(new RichMapFunction<TradeProvinceOrderWindow, TradeProvinceOrderWindow>() {

            private ValueState<String> orderIdValueState;

            @Override
            public void open(Configuration parameters) throws Exception {
                StateTtlConfig ttlConfig = new StateTtlConfig.Builder(Time.seconds(5))
                        .setUpdateType(StateTtlConfig.UpdateType.OnReadAndWrite)
                        .build();
                ValueStateDescriptor<String> stateDescriptor = new ValueStateDescriptor<>("order-state", String.class);
                stateDescriptor.enableTimeToLive(ttlConfig);

                orderIdValueState = getRuntimeContext().getState(stateDescriptor);
            }

            @Override
            public TradeProvinceOrderWindow map(TradeProvinceOrderWindow value) throws Exception {

                String state = orderIdValueState.value();

                if (state == null) {
                    orderIdValueState.update("1");
                    value.setOrderCount(1L);
                } else {
                    value.setOrderCount(0L);
                }

                return value;
            }
        });

        //TODO 7.按照省份id分组  开窗  聚合
        SingleOutputStreamOperator<TradeProvinceOrderWindow> reduceDS = tradeProvinceWithOrderCtDS.keyBy(TradeProvinceOrderWindow::getProvinceId).window(TumblingEventTimeWindows.of(org.apache.flink.streaming.api.windowing.time.Time.seconds(10))).reduce(new ReduceFunction<TradeProvinceOrderWindow>() {
            @Override
            public TradeProvinceOrderWindow reduce(TradeProvinceOrderWindow value1, TradeProvinceOrderWindow value2) throws Exception {
                value1.setOrderAmount(value1.getOrderAmount().add(value2.getOrderAmount()));
                value1.setOrderCount(value1.getOrderCount() + value2.getOrderCount());
                return value1;
            }
        }, new WindowFunction<TradeProvinceOrderWindow, TradeProvinceOrderWindow, String, TimeWindow>() {
            @Override
            public void apply(String s, TimeWindow window, Iterable<TradeProvinceOrderWindow> input, Collector<TradeProvinceOrderWindow> out) throws Exception {

                WindowFunctionUtil.set(input, window, out);

            }
        });
        reduceDS.print("reduceDS--->");

        //TODO 8.关联维度
        SingleOutputStreamOperator<TradeProvinceOrderWindow> resultDS = AsyncDataStream.unorderedWait(reduceDS,
                new DimAsyncFunction<TradeProvinceOrderWindow>("DIM_BASE_PROVINCE") {
            @Override
            public String getkey(TradeProvinceOrderWindow input) throws Exception {
                return input.getProvinceId();
            }

            @Override
            public void join(TradeProvinceOrderWindow input, JSONObject dimInfo) throws Exception {

                input.setProvinceName(dimInfo.getString("NAME"));
            }
        }, 60, TimeUnit.SECONDS);

        //TODO 9.写出
        resultDS.print("resultDS--->");
        resultDS.addSink(MyClickHouseUtil.getSinkFunction("insert into dws_trade_province_order_window values(?,?,?,?,?,?,?)"));

        //TODO 10.执行
        env.execute("Dws10_TradeProvinceOrderWindow_01");

    }
}
